package com.example.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;

/**
 * @author 游家纨绔
 * @dateTime 2024-03-31 15:00
 * @apiNote TODO
 */
@WebServlet(name = "Servlet", urlPatterns = "/Servlet")
public class CustomerServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		// 设置响应的字符集
		resp.setContentType("text/html;charset=utf-8");
		// 设置响应头允许ajax跨域访问
		resp.setHeader("Access-Control-Allow-Origin", "*");
		// 星号表示所有的异域请求都可以接受
		resp.setHeader("Access-Control-Allow-Methods", "GET,POST");
		// 获取微信小程序传递的参数值并打印
		String transInfo = req.getParameter("transInfo");
		System.out.println("接收到小程序端传递的数据:" + transInfo);
		// 向小程序端传递数据
		Writer out = resp.getWriter();
		out.write("呐，这是后台给小程序端的数据");
		out.flush();
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		super.doPost(req, resp);
	}

}
